package com.wz.project.bas.warehouse.controller;

import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.project.bas.warehouse.domain.BasWarehouse;
import com.wz.project.bas.warehouse.service.WarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("bas/warehouse")
public class WarehouseController extends BaseController {

    private String prefix = "bas/warehouse";

    @Autowired
    WarehouseService warehouseService;

    @RequiresPermissions("bas:warehouse:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/warehouse";
    }

    @PostMapping("/list")
    @ResponseBody
    public Object getList(BasWarehouse warehouse){

    startPage();
    List<BasWarehouse> list=warehouseService.selectWarehouseList(warehouse);
    System.out.println(list);
    return getDataTable(list);
}

    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody

    public AjaxResult remove(String ids)
    {
        return toAjax(warehouseService.deleteWarehouseByIds(ids));
    }

    /**
     * 修改项目
     * @param warehouse_id
     * @param mmap
     * @return
     */
    @RequiresPermissions("bas:warehouse:edits")
    @GetMapping("/edit/{warehouse_id}")
    public String edit(@PathVariable("warehouse_id") String warehouse_id, ModelMap mmap)
    {
        BasWarehouse basWarehouse=warehouseService.selectWarehouseByWarehouseId(warehouse_id);
//        List<Prjgroup> prjgroups = prjGroupService.selectPrjgroupByDeptId(basWarehouse.getWarehouse_id());
        mmap.put("basWarehouse", basWarehouse);
//        mmap.put("prjgroups",prjgroups);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:item:saveAndUpdate")
    @PostMapping("/saveAndUpdate")
    @ResponseBody
    public AjaxResult saveAndUpdate(@Validated BasWarehouse basWarehouse)
    {

        return toAjax(warehouseService.saveAndUpdate(basWarehouse));
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        /*mmap.put("roles", roleService.selectRoleAll().stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        mmap.put("posts", postService.selectPostAll());*/
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasWarehouse basWarehouse)
    {
//        item.setTheProblem("无");
        String s = String.valueOf(System.currentTimeMillis());
        String substring = s.substring(s.length() - 6);
//        int pejId = Integer.parseInt(substring);
        basWarehouse.setWarehouse_id(substring);
        return toAjax(warehouseService.insertWarehouse(basWarehouse));
    }


}
